(add-hook 'html-mode-hook '(lambda ()
                             (progn (orgtbl-mode)
                                    (tabkey2-mode))))

(add-hook 'text-mode-hook 'turn-off-auto-fill)


(add-hook 'emacs-lisp-mode-hook
          (lambda ()
            (local-set-key [return] 'newline-and-indent)
            (local-set-key [backspace] 'c-hungry-backspace)
            (outline-minor-mode)
            (company-mode)
            ;; (flyspell-prog-mode)
            ))

(add-hook 'lisp-interaction-mode
          (lambda ()
            (local-set-key [return] 'newline-and-indent)
            (local-set-key [backspace] 'c-hungry-backspace)
            (company-mode)
            ;; (flyspell-prog-mode)
            'my-c-mode-auto-pair))
(add-hook 'java-mode-hook 'outline-minor-mode)
(add-hook 'perl-mode-hook 'outline-minor-mode)
(add-hook 'shell-script-mode 'my-c-mode-auto-pair)
(add-hook 'find-file-hook 'flymake-find-file-hook)
(add-hook 'font-lock-mode-hook 'my-doxymacs-font-lock-hook)
(add-hook 'post-command-hook 'ue-selection-update-cont)
(add-hook 'eshell-mode-hook '(lambda ()
                               (local-set-key (kbd "C-a") 'eshell-bol-maybe-my)
                               (eshell-add-scroll-to-bottom)))
(add-hook 'flyguess-guessed-hook (lambda (dictionary)
                                   (ispell-change-dictionary dictionary)
                                   (flyspell-mode 1)
                                   (flyspell-buffer)))

(add-hook 'comint-output-filter-functions
          'comint-watch-for-password-prompt)

(add-hook 'lisp-mode-hook 'my-c-mode-auto-pair)
(add-hook 'emacs-lisp-mode-hook 'my-c-mode-auto-pair)

(add-hook 'html-mode-hook '(lambda ()
                             (setq mmm-classes '(php-in-html))
                             (set-face-background
                              'mmm-default-submode-face "Blank")
                             (mmm-mode-on)))


(add-hook 'org-mode-hook 'outline-minor-mode)
(add-hook 'org-mode-hook 'turn-off-auto-fill)
(add-hook 'write-file-hooks 'time-stamp)
(add-hook 'remember-mode-hook 'delete-other-windows)


(add-hook 'gnus-article-prepare-hook
          (lambda ()
            (setq fill-column 60)
            (gnus-article-date-local)))

(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)

(add-hook 'ido-define-mode-map-hook 'ido-my-keys)
(add-hook 'picture-mode-hook 'rm-example-picture-mode-bindings) 

;; (setq sql-interactive-mode-hook
;;       (lambda ()
;;         (define-key sql-interactive-mode-map "\t" 'comint-dynamic-complete)
;;         (sql-mysql-completion-init)))

(setq ibuffer-mode-hook
      (lambda ()
        (setq ibuffer-filter-groups
              '(
                ("*buffer*" (name . "\\*.*\\*"))
                ("*dired*" (mode . dired-mode))
                ;;                ("*scratch*" (mode . lisp-interaction-mode))
                ("tex" (mode . latex-mode))
                ("org" (mode . org-mode))
                ("php" (or (mode . php-mode)
                           (mode . html-mode)))
                ("text" (mode . text-mode))
                ;; ("muse" (mode . muse-mode))
                ("elisp" (or (mode . emacs-lisp-mode)
                             (mode . lisp-interaction-mode)))
                ("c/c++" (or (mode . c++-mode)
                             (mode . c-mode)
                             (mode . cuda-mode)))
                ("java" (mode . jde-mode))
                ("python" (mode . python-mode))
                ("fortran" (or (mode . fortran-mode)
                               (mode . f90-mode)))
                ("shell" (or (mode . shell-script-mode)
                             (mode . sh-mode)))
                ("matlab" (mode . matlab-mode))
                ("makefile" (or (mode . makefile-mode)
                                (mode . makefile-gmake-mode)
                                (mode . makefile-bsdmake-mode)
                                (mode . makefile-automake-mode)))

                ))))


;;; (setq mathematica-m-mode-hook (function (lambda () (interactive)
;;;                                           (setq mathematica-m-mode-hook-done 1)
;;;                                           (show-paren-mode)  
;;;                                           )))


(add-hook 'dired-mode-hook
          (lambda ()
            ;; dired use `s' to switch sort by name/time, we undefine it so
            ;; that it can be used as prefix
            (define-key dired-mode-map
              (kbd "s")
              nil)
            (defvar kid-dired-sort-reverse nil
              "sort reversely")
            (defvar kid-dired-sort-recursive nil
              "sort recursively")
            (kid-dired-define-sort "X" "X")
            (kid-dired-define-sort "t" "t")
            (kid-dired-define-sort "S" "S")
            (kid-dired-define-sort "U" "U")
            (kid-dired-define-sort "u" "ut") ; sort by access time
            (kid-dired-define-sort "c" "ct") ; sort by ctime
            (kid-dired-define-sort "n" "")   ; sort by name :)
            (kid-dired-define-toggle "r" kid-dired-sort-reverse)
            (setq dired-omit-files "^#\\|^\\..*")
            (dired-omit-mode -1)
            (define-key dired-mode-map (kbd "RET") 'joc-dired-single-buffer)
            (define-key dired-mode-map (kbd "<mouse-1>") 'joc-dired-single-buffer-mouse)
            (define-key dired-mode-map (kbd "^")
              (lambda ()
                (interactive)
                (joc-dired-single-buffer "..")))
            (define-key dired-mode-map (kbd "W") 'my-browse-with-emacs-w3m)
            (kid-dired-define-toggle "R" kid-dired-sort-recursive)))

(add-hook 'dired-after-readin-hook 'his-dired-sort)


(setq my-shebang-patterns
      (list "^#!/usr/.*/perl\\(\\( \\)\\|\\( .+ \\)\\)-w *.*"
            "^#!/usr/.*/sh"
            "^#!/usr/.*/bash"
            "^#!/usr/.*/expect"
            "^#!/bin/sh"
            "^#!/.*/perl"
            "^#!/.*/awk"
            "^#!/.*/sed"
            "^#!/.*/env"
            "^#!/.*/python"
            "^#!/bin/bash"))
(add-hook
 'after-save-hook
 (lambda ()
   (if (not (= (shell-command (concat "test -x " (buffer-file-name))) 0))
       (progn
         ;; This puts message in *Message* twice, but minibuffer
         ;; output looks better.
         (message (concat "Wrote " (buffer-file-name)))
         (save-excursion
           (goto-char (point-min))
           ;; Always checks every pattern even after
           ;; match.  Inefficient but easy.
           (dolist (my-shebang-pat my-shebang-patterns)
             (if (looking-at my-shebang-pat)
                 (if (= (shell-command
                         (concat "chmod u+x " (buffer-file-name)))
                        0)
                     (message (concat
                               "Wrote and made executable "
                               (buffer-file-name))))))))
     ;; This puts message in *Message* twice, but minibuffer output
     ;; looks better.
     (message (concat "Wrote " (buffer-file-name))))))
(add-hook 'message-mode-hook '(lambda ()
                                (progn
                                  (orgtbl-mode)
                                  (flyspell-mode)
                                  ;; (when (require 'auto-complete nil t)
                                  ;;   (make-variable-buffer-local 'ac-sources)
                                  ;;   (add-to-list 'ac-sources 'ac-source-yasnippet))
                                  (company-mode)
                                  (local-set-key (kbd "C-z C-a") 'mail-abbrev-insert-alias)
                                  )))
;;;;;;;;;;;;;;;;;;;;  junk  ;;;;;;;;;;;;;;;;;;;;
;;(add-hook 'lisp-interaction-mode-hook ')

;; (add-hook 'inferior-octave-mode-hook
;;           (lambda ()
;;             (turn-on-font-lock)
;;             (define-key inferior-octave-mode-map [up]
;;               'comint-previous-input)
;;             (define-key inferior-octave-mode-map [down]
;;               'comint-next-input)))
;; (color-theme-dimilartty)
